package com.happypets.servicesImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.happypets.dao.PetDao;
import com.happypets.daoImpl.PetDaoImpl;
import com.happypets.model.EvaluateBean;
import com.happypets.model.PetBean;
import com.happypets.services.PetService;

public class PetServiceImpl implements PetService {

	PetDao petDao = new PetDaoImpl();

	public boolean isState(String id) {
		return false;
	}

	public boolean isState(PetBean pet) {
		return false;
	}

	public boolean isMark(String id) {
		return false;
	}

	public boolean isMark(PetBean pet) {
		return false;
	}

	public boolean isSaled(String id) {
		return false;
	}

	public boolean isSaled(PetBean pet) {
		return false;
	}

	public List<PetBean> bigCategoryPets(String bigCategory) {

		String sql = "select a.* from animalsinfo a where a.bigcategoryid = " + bigCategory;

		List<Map<String, String>> petList = (List<Map<String, String>>) petDao.query(sql);

		int size = petList.size();

		List<PetBean> pets = new ArrayList<PetBean>();

		for (int i = 0; i < size; i++) {

			PetBean pet = mapToPetBean(petList.get(i));

			pets.add(pet);

		}
		return pets;
	}

	public List<String> getSmallCategories(String bigCategory) {
		
//		String sql = "select t.smallcategory from SMALLCATEGORY t where t.bigcategory=(select t.bigcategory from BIGCATEGORY t where t.name='"+bigCategory+"')";
		
		return null;
	}
	
	public List<Map<String,String>> smallCategories(String bigCategory) {
		
		String sql = "select t.name from SMALLCATEGORY t where t.bigcategory=(select t.bigcategory from BIGCATEGORY t where t.name='"+bigCategory+"')";
		System.out.println(sql);
		return (List<Map<String, String>>) petDao.query(sql);
	}

	public List<PetBean> bigCategoryPets(String bigCategory, int size) {

		String sql = "select a.* from animalsinfo a where a.bigcategoryid = " + bigCategory + " and rownum <=" + size +" and a.animal_saled ='1'";
		
		System.out.println(sql);

		List<Map<String, String>> petList = (List<Map<String, String>>) petDao.query(sql);
		// 防止实际值太小
		size = petList.size();

		List<PetBean> pets = (List<PetBean>) listToList(petList);

		return pets;
	}

	public List<PetBean> smallCategoey(String smallCategory) {
		return null;
	}

	public List<PetBean> smallCategoey(String smallCategory, int size) {
		return null;
	}



	@Override
	public List<?> query(String sql) {

		return petDao.query(sql);
	}
	
	public String bigcategoryIdByName(String name){
		
		String sql = "select t.bigcategory from bigcategory t where t.name='"+name+"'";
		
		
		return petDao.getString(sql);
	}
	
	public String smallcategoryIdByName(String name){
		
		String sql = "select t.smallcategory from smallcategory t where t.name='"+name+"'";
		
		return petDao.getString(sql);
	}

	@Override
	public int addPet(PetBean pet) {

		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		
		String name = pet.getName();
		int big = pet.getBigCategory();
		int small = pet.getSmallCategory();
		String nickName = pet.getNickName();
		String sex = pet.getSex();
		String age = pet.getAge();
		String shape = pet.getShape();
		String weight = pet.getWeight();
		String color = pet.getColor();
		String costPrice = pet.getCostPrice();
		String totalNumber = "1";
		String address = pet.getAddress();
		String nowPrice = pet.getNowPrice();
		String specialPrice = pet.getSpecialPrice();
		String img1 = pet.getImage1();
		String img2 = pet.getImage2();
		String img3 = pet.getImage3();
		String introduce = pet.getIntroduce();
		int mark = pet.getMark();
		int state = pet.getState();
				
		
		String sql = "insert into animalsinfo("
				+ "id,bigcategoryid,smallcategoryid,animal_name,animal_nickname,animal_sex,"
				+ "animal_age,animal_shape,animal_weight,animal_color,animal_costprice,"
				+ "animal_totalnumber,animal_address,animal_nowprice,animal_specialprice,animal_image01,"
				+ "animal_image02,animal_image03,animal_publictime,animal_introduce,animal_mark,"
				+ "animal_state,animal_boughttimes,animal_saled)values(animalsinfo_seq.nextval,"
				+big+","+small+",'"+name+"','"+nickName+"','"+sex+"',"
						+ "'"+age+"','"+shape+"','"+weight+"','"+color+"','"+costPrice+"',"
								+ "'"+totalNumber+"','"+address+"','"+nowPrice+"','"+specialPrice+"','"+img1+"',"
										+ "'"+img2+"','"+img3+"',to_date('"+sdf.format(new Date())+"','YYYY-MM-DD HH24:MI:SS'),'"+introduce+"','"+mark+"',"
												+ "'"+state+"','1','1')";
		
		
//		System.out.println(sql);
		
		
		return petDao.excutQuery(sql);
	}
	
	
	public int deletePet(String id) {
		
		String sql = "delete animalsinfo a where a.id="+id;
		
		return petDao.excutQuery(sql);
	}

	@Override
	public int deletePet(PetBean pet) {
		String id = pet.getId();
		String sql = "delete animalsinfo a where a.id="+id;
		
		return petDao.excutQuery(sql);
	}

	@Override
	public int updatePetInfo(PetBean pet) {
		
		String id = pet.getId();
		String name = pet.getName();
		int big = pet.getBigCategory();
		int small = pet.getSmallCategory();
		String nickName = pet.getNickName();
		String sex = pet.getSex();
		String age = pet.getAge();
		String shape = pet.getShape();
		String weight = pet.getWeight();
		String color = pet.getColor();
		String costPrice = pet.getCostPrice();
		String totalNumber = "1";
		String address = pet.getAddress();
		String nowPrice = pet.getNowPrice();
		String specialPrice = pet.getSpecialPrice();
		String introduce = pet.getIntroduce();
		int mark = pet.getMark();
		int state = pet.getState();
		
		String sql="update animalsinfo a "
				+ "set a.animal_name='"+name+"',a.animal_nickname='"+nickName+"',a.bigcategoryid='"+big+"',a.smallcategoryid='"+small+"',a.animal_sex='"+sex+"',a.animal_age='"+age+"',a.animal_shape='"+shape+"',"
				+ "a.animal_weight='"+weight+"',a.animal_color='"+color+"',a.animal_address='"+address+"',a.animal_nowprice='"+nowPrice+"',a.animal_specialprice='"+specialPrice+"',"
				+ "a.animal_introduce='"+introduce+"',a.animal_mark='"+mark+"',a.animal_state='"+state+"' where a.id="+id;
		
//		System.out.println(sql);
		
		return petDao.excutQuery(sql);
	}
	
	
	public int updatePetImage(PetBean pet) {

		String id = pet.getId();
		
		String img1 = pet.getImage1();
		String img2 = pet.getImage2();
		String img3 = pet.getImage3();
		
		String sql = "update animalsinfo a set a.animal_image01='"+img1+"',a.animal_image02='"+img2+"',a.animal_image03='"+img3+"' where a.id="+id;
		
		System.out.println(sql);
		
		return petDao.excutQuery(sql);
	}
	
	

	@Override
	public List<?> pageQuery(int petKind, int showNumber, int currentPageNumber) {

		String sql = "select * from (select a.*,rownum as rownu from animalsinfo a  where a.bigcategoryid = " + petKind
				+ " and rownum <=" + (currentPageNumber * showNumber) + ") where rownu > "
				+ ((currentPageNumber - 1) * showNumber);

		System.out.println("sql = " + sql);

		List<Map<String, String>> petList = (List<Map<String, String>>) petDao.query(sql);

		List<PetBean> pets = (List<PetBean>) listToList(petList);

		return pets;
	}

	/**
	 * 将listmap转换为list
	 * 
	 * @param list
	 * @return
	 */
	public List<?> listToList(List<Map<String, String>> list) {

		int size = list.size();

		List<PetBean> pets = new ArrayList<PetBean>();

		for (int i = 0; i < size; i++) {

			PetBean pet = new PetBean();

			Map<String, String> map = list.get(i);

			pet.setId(map.get("ID"));
			pet.setBigCategory(Integer.parseInt(map.get("BIGCATEGORYID")));
			pet.setSmallCategory(Integer.parseInt(map.get("SMALLCATEGORYID")));
			pet.setName(map.get("ANIMAL_NAME"));
			pet.setNickName(map.get("ANIMAL_NICKNAME"));
			pet.setSex(map.get("ANIMAL_SEX"));
			pet.setAge(map.get("ANIMAL_AGE"));
			pet.setShape(map.get("ANIMAL_SHAPE"));
			pet.setWeight(map.get("ANIMAL_WEIGHT"));
			pet.setColor(map.get("ANIMAL_COLOR"));
			pet.setCostPrice(map.get("ANIMAL_COSTPRICE"));
			pet.setTotalNumber(map.get("ANIMAL_TOTALNUMBER"));
			pet.setAddress(map.get("ANIMAL_ADDRESS"));
			pet.setNowPrice(map.get("ANIMAL_NOWPRICE"));
			pet.setSpecialPrice(map.get("ANIMAL_SPECIALPRICE"));
			pet.setImage1(map.get("ANIMAL_IMAGE01"));
			pet.setImage2(map.get("ANIMAL_IMAGE02"));
			pet.setImage3(map.get("ANIMAL_IMAGE03"));
			pet.setBoughtTimes(map.get("ANIMAL_BOUGHTTIMES"));
			pet.setPublicTime(map.get("ANIMAL_PUBLICTIME"));
			pet.setIntroduce(map.get("ANIMAL_INTRODUCE"));
			pet.setMark(Integer.parseInt(map.get("ANIMAL_MARK")));
			pet.setState(Integer.parseInt(map.get("ANIMAL_STATE")));
			pet.setSaled(Integer.parseInt(map.get("ANIMAL_SALED")));

			pets.add(pet);
		}
		return pets;

	}

	/**
	 * 将map转换成petbean 针对数据库
	 * 
	 * @param map
	 * @return
	 */
	public PetBean mapToPetBean(Map<String, String> map) {

		PetBean pet = new PetBean();

		pet.setId(map.get("ID"));
		pet.setBigCategory(Integer.parseInt(map.get("BIGCATEGORYID")));
		pet.setSmallCategory(Integer.parseInt(map.get("SMALLCATEGORYID")));
		pet.setName(map.get("ANIMAL_NAME"));
		pet.setNickName(map.get("ANIMAL_NICKNAME"));
		pet.setSex(map.get("ANIMAL_SEX"));
		pet.setAge(map.get("ANIMAL_AGE"));
		pet.setShape(map.get("ANIMAL_SHAPE"));
		pet.setWeight(map.get("ANIMAL_WEIGHT"));
		pet.setColor(map.get("ANIMAL_COLOR"));
		pet.setCostPrice(map.get("ANIMAL_COSTPRICE"));
		pet.setTotalNumber(map.get("ANIMAL_TOTALNUMBER"));
		pet.setAddress(map.get("ANIMAL_ADDRESS"));
		pet.setNowPrice(map.get("ANIMAL_NOWPRICE"));
		pet.setSpecialPrice(map.get("ANIMAL_SPECIALPRICE"));
		pet.setImage1(map.get("ANIMAL_IMAGE01"));
		pet.setImage2(map.get("ANIMAL_IMAGE02"));
		pet.setImage3(map.get("ANIMAL_IMAGE03"));
		pet.setBoughtTimes(map.get("ANIMAL_BOUGHTTIMES"));
		pet.setPublicTime(map.get("ANIMAL_PUBLICTIME"));
		pet.setIntroduce(map.get("ANIMAL_INTRODUCE"));
		pet.setMark(Integer.parseInt(map.get("ANIMAL_MARK")));
		pet.setState(Integer.parseInt(map.get("ANIMAL_STATE")));
		pet.setSaled(Integer.parseInt(map.get("ANIMAL_SALED")));

		return pet;

	}
	
	
	/**
	 * 将map转换成petbean 针对表格
	 * 
	 * @param map
	 * @return
	 */
	public PetBean mapToPetBean2(Map<String, String> map) {

		PetBean pet = new PetBean();

		
		if(map.get("pet-id")!=null){
			pet.setId(map.get("pet-id"));
		}
		
		System.out.println(map.get("pet-bigcategory")+"---"+map.get("pet-smallcategory"));
		
		pet.setBigCategory(Integer.parseInt(bigcategoryIdByName(map.get("pet-bigcategory"))));
		pet.setSmallCategory(Integer.parseInt(smallcategoryIdByName(map.get("pet-smallcategory"))));
		pet.setName(map.get("pet-name"));
		pet.setNickName(map.get("pet-nickname"));
		pet.setSex(map.get("pet-sex"));
		pet.setAge(map.get("pet-age"));
		pet.setShape(map.get("pet-shape"));
		pet.setWeight(map.get("pet-weight"));
		pet.setColor(map.get("pet-color"));
		pet.setCostPrice(map.get("pet-costprice"));
//		pet.setTotalNumber(map.get("pet-totalnumber"));
		pet.setAddress(map.get("pet-address"));
		pet.setNowPrice(map.get("pet-price"));
		pet.setSpecialPrice(map.get("pet-specialprice"));
		
		if(map.get("pet-image01") != null){
			pet.setImage1(map.get("pet-image01"));
			pet.setImage2(map.get("pet-image02"));
			pet.setImage3(map.get("pet-image03"));
		}
		
		if(map.get("pet-date")!=null){
			pet.setPublicTime(map.get("pet-date"));
		}

		
		pet.setIntroduce(map.get("pet-introduce"));
		pet.setMark(Integer.parseInt(map.get("pet-mark")));
		pet.setState(Integer.parseInt(map.get("pet-state")));
//		pet.setSaled(Integer.parseInt(map.get("ANIMAL_SALED")));
//		pet.setBoughtTimes(map.get("pet-boughttimes"));

		return pet;

	}
	
	
	
	/**
	 * 将map转换成petbean 针对表格
	 * 仅有图片
	 * @param map
	 * @return
	 */
	public PetBean mapToPetBean3(Map<String, String> map) {

		PetBean pet = new PetBean();

		
			pet.setId(map.get("pet-id"));

			pet.setImage1(map.get("pet-image01"));
			pet.setImage2(map.get("pet-image02"));
			pet.setImage3(map.get("pet-image03"));
		


		return pet;

	}
	
	

	public PetBean getPetBean(String id) {

		String sql = "select a.* from animalsinfo a where id = " + id;

		System.out.println(sql);

		List<Map<String, String>> petList = (List<Map<String, String>>) petDao.query(sql);

		System.out.println(petList.size());

		Map<String, String> petMap = petList.get(0);

		return mapToPetBean(petMap);

	}

	@Override
	public List<?> pageSearchQuery(int petKind, int showNumber, int currentPageNumber, Map<String, String> keyMap) {

		// String shapesql = "select * from (select t.*,rownum as rownu from
		// animalsinfo t where t.bigcategoryid = "+petKind+" and
		// t.animal_shape='"+shape+"' and rownum<=" + (currentPageNumber *
		// showNumber) + ") where rownu >= "+((currentPageNumber - 1) *
		// showNumber);
		// String colorsql = "select * from (select t.*,rownum as rownu from
		// animalsinfo t where t.bigcategoryid = "+petKind+" and
		// t.animal_color='"+color+"' and rownum<=" + (currentPageNumber *
		// showNumber) + ") where rownu >="+((currentPageNumber - 1) *
		// showNumber);
		// String categorysql = "select * from (select a.*,rownum as rownu from
		// animalsinfo a right join (select smallcategory from smallcategory
		// where name='"+category+"' and rownum<=" + (currentPageNumber *
		// showNumber) + ") b on a.smallcategoryid=b.smallcategory) where
		// rownu>="+((currentPageNumber - 1) * showNumber);
		// String keywords = "";暂时不写
		String sql = "select * from (select a.*,rownum as rownu from animalsinfo a  where a.bigcategoryid = " + petKind
				+ " and rownum <=" + (currentPageNumber * showNumber) + ") where rownu > "
				+ ((currentPageNumber - 1) * showNumber);

		if (keyMap.containsKey("shape")) {
			String shape = keyMap.get("shape");
			sql = "select * from (select t.*,rownum as rownu from animalsinfo t where t.bigcategoryid = " + petKind
					+ " and t.animal_shape='" + shape + "' and rownum<=" + (currentPageNumber * showNumber)
					+ ") where rownu > " + ((currentPageNumber - 1) * showNumber);
		}
		if (keyMap.containsKey("color")) {
			String color = keyMap.get("color");
			sql = "select * from (select t.*,rownum as rownu from animalsinfo t where t.bigcategoryid = " + petKind
					+ " and t.animal_color='" + color + "' and rownum<=" + (currentPageNumber * showNumber)
					+ ") where rownu > " + ((currentPageNumber - 1) * showNumber);
		}
		if (keyMap.containsKey("category")) {
			String category = keyMap.get("category");
			sql = "select * from (select a.*,rownum as rownu from animalsinfo a right join (select smallcategory from smallcategory where name='"
					+ category + "' and rownum<=" + (currentPageNumber * showNumber)
					+ ") b on a.smallcategoryid=b.smallcategory) where rownu>"
					+ ((currentPageNumber - 1) * showNumber);
		}
		//	缺少keyword
		System.out.println("sql = " + sql);

		List<Map<String, String>> petList = (List<Map<String, String>>) petDao.query(sql);

		List<PetBean> pets = (List<PetBean>) listToList(petList);

		return pets;
	}
	
	/**
	 * 搜索的结果个数
	 * @param keyword
	 * @return
	 */
	public int searchPetCount(String keyword){
		
		String sql = "select p.ID "
       +" from petallinfo p "
            +" where ( p.bname like '%"+keyword+"%' or p.sname like '%"+keyword+"%' or p.ANIMAL_NAME like '%"+keyword+"%' or "
                  +" p.ANIMAL_NICKNAME like '%"+keyword+"%' or "
                   +"p.ANIMAL_SHAPE like '%"+keyword+"%' or p.ANIMAL_COLOR like '%"+keyword+"%' or p.ANIMAL_ADDRESS like '%"+keyword+"%' or "
                  +"p.ANIMAL_INTRODUCE like '%"+keyword+"%')";
		
		
		System.out.println("搜索结果用sql："+sql);
		
		return petDao.query(sql).size();
		
	}
	
	
	/**
	 * 获取搜索结果
	 * @param showNumber 每页显示
	 * @param currentPageNumber 当前页数
	 * @param keyword 关键词
	 * @return
	 */
	public List<PetBean> searchPageResult( int showNumber, int currentPageNumber,String keyword){
		
		String sql = "select * from ("
				+ "select p.*,rownum as rowno "
							      + " from petallinfo p "
							      +" where ( p.bname like '%"+keyword+"%' or p.sname like '%"+keyword+"%' or p.ANIMAL_NAME like '%"+keyword+"%' or "
				                  +" p.ANIMAL_NICKNAME like '%"+keyword+"%' or "
				                   +"p.ANIMAL_SHAPE like '%"+keyword+"%' or p.ANIMAL_COLOR like '%"+keyword+"%' or p.ANIMAL_ADDRESS like '%"+keyword+"%' or "
				                  +"p.ANIMAL_INTRODUCE like '%"+keyword+"%')"
							                  + "and rownum <=" + (currentPageNumber * showNumber)
							                  + ") where rowno >"+((currentPageNumber - 1) * showNumber);
		
		System.out.println("搜索用sql："+sql);
		
		List<Map<String, String>> petList = (List<Map<String, String>>) petDao.query(sql);

		List<PetBean> pets = (List<PetBean>) listToList(petList);

		return pets;
		
		
	}
	
	
	
	
	
	/**
	 * 分页获取评论
	 * @param id
	 * @param showNumber
	 * @param currentPageNumber
	 * @return
	 */
	public List<EvaluateBean> pageEvalutesQuery(int id, int showNumber, int currentPageNumber){
		
		//获取评论
		//修改
		String sql = "select * from "
						+ "(select c.*,b.sale_money,a.evaluate_content,a.evaluate_date,a.evaluate_star,rownum as rownu from "
						+ "(select v.vip_name from vipinfo v where v.vip_id=(select s.buyer_id from salehistory s where s.animal_id="+id+"))"
						+ " c ,vipevaluates a left join salehistory b on a.id=b.saled_id  where a.id="+id+" and rownum<="+(currentPageNumber * showNumber)
						+" ) where rownu>"+((currentPageNumber - 1) * showNumber);
		
		System.out.println(sql);
		
		List<Map<String,String>> eva = (List<Map<String, String>>) petDao.query(sql);
		
		List<EvaluateBean> le = new ArrayList<EvaluateBean>();
		
		for (int i = 0; i < eva.size(); i++) {
			Map<String,String> map = eva.get(i);
			EvaluateBean eb = new EvaluateBean();
			
			eb.setBuyerName(map.get("VIP_NAME"));
			eb.setEvaluateDate(map.get("EVALUATE_DATE"));
			eb.setPrice(map.get("SALE_MONEY"));
			eb.setStart(map.get("EVALUATE_STAR"));
			eb.setEvaluate(map.get("EVALUATE_CONTENT"));
			le.add(eb);
		}
		
		
		return le;
		
	}
	
	/**
	 * 获取列表
	 * @return list
	 */
	public List<Map<String,String>> list(){
		
		String sql = "select t.* from BIGCATEGORY t";
		
		System.out.println(sql);
		
		return (List<Map<String, String>>) petDao.query(sql);
		
	}

	
	
	/**
	 * 获取所有所需页数,需要传入获取的类型
	 * @param flag
	 * @return 数量
	 */
	public int AllPages(int flag,int pageShowNumber){
		/*** 
		 *  1为pet  
		 *  2为user
		 *  3为manager
		 *  4为订单 salehistory
		 *   ***/
		
		String sql="";
		
		switch(flag){
		case 1: 
			sql="select t.id from ANIMALSINFO t ";
			break;
		case 2:
			sql="select t.vip_id from VIPINFO t";
			break;
		case 3:
			sql="select b.manager_id from managerinfo b";
			break;
		case 4:
			sql="select a.saled_id from salehistory a";
			break;
		}
		
		int t = petDao.query(sql).size();
		
		
		return t%pageShowNumber==0?t/pageShowNumber:t/pageShowNumber+1;
		
	}
	
	
	/**
	 * 获取宠物的评价
	 * @param id
	 * @return
	 */
	public List<Map<String,String>> petComm(String id){
		String sql = "select g.vip_name,e.money,f.evaluate_content,f.evaluate_date,f.evaluate_star from "
				+ "(select d.buyer_id as id1,d.saled_id as id2,d.sale_money as money from"
				+ " (select b.id as ids from animalsinfo b where b.smallcategoryid="
				+ "(select a.smallcategoryid from animalsinfo a where a.id="+id+")) c  "
				+ "left join salehistory d on c.ids = d.saled_id ) e left join vipevaluates f on e.id2=f.id left join vipinfo g on e.id1 = g.vip_id";
		
		return (List<Map<String, String>>) petDao.query(sql);
		
	}
	
	/**
	 * 获取销售记录
	 * @param id
	 * @return
	 */
	public List<Map<String,String>> petSaleHis(String id){
		String sql = "select v.vip_name as names , t.sale_money as money ,"
				+ " t.sale_date as dates from salehistory t left join vipinfo v on t.buyer_id=v.vip_id where t.animal_id="+id;
		
		return (List<Map<String, String>>) petDao.query(sql);
		
	}
	
	

	/**
	 * 按小时统计
	 * @param start
	 * @param end
	 * @return 数据
	 */
	public List<Map<String,String>> statsByHour(String start,String end){
		
		String sql = "select nvl(a.insums,0) as inmoney,nvl(b.outsums,0) as outmoney,nvl(a.dates,b.dates) as dates from "+
							"(select sum(t.sale_money) as insums, trunc(t.sale_date, 'HH') as dates from salehistory t "+
							"where to_char(t.sale_date,'yyyy/mm/dd hh24:mi:ss')>='"+start+"' and to_char(t.sale_date,'yyyy/mm/dd hh24:mi:ss')<'"+end+"' group by trunc(t.sale_date, 'HH') "+
							") a full join (select sum(c.animal_costprice) as outsums, trunc(c.animal_publictime, 'HH') as dates from animalsinfo c "+
							"where to_char(c.animal_publictime,'yyyy/mm/dd hh24:mi:ss')>='"+start+"' and to_char"
							+ "(c.animal_publictime,'yyyy/mm/dd hh24:mi:ss')<'"+end+"' group by trunc(c.animal_publictime, 'HH')  ) b on a. dates = b.dates";
									
		
		return (List<Map<String, String>>) petDao.query(sql);
		
	}
	
	
	/**
	 * 按天统计
	 * @param start
	 * @param end
	 * @return 数据
	 */
	public List<Map<String,String>> statsByDay(String start,String end){
		
		String sql ="select nvl(a.insums,0) as inmoney,nvl(b.outsums,0) as outmoney,nvl(a.dates,b.dates) as dates from "+
				"(select sum(t.sale_money) as insums, trunc(t.sale_date, 'DD') as dates from salehistory t "+
				"where to_char(t.sale_date,'yyyy/mm/dd hh24:mi:ss')>='"+start+"' and to_char(t.sale_date,'yyyy/mm/dd hh24:mi:ss')<'"+end+"' group by trunc(t.sale_date, 'DD') "+
				") a full join (select sum(c.animal_costprice) as outsums, trunc(c.animal_publictime, 'DD') as dates from animalsinfo c "+
				"where to_char(c.animal_publictime,'yyyy/mm/dd hh24:mi:ss')>='"+start+"' and to_char"
				+ "(c.animal_publictime,'yyyy/mm/dd hh24:mi:ss')<'"+end+"' group by trunc(c.animal_publictime, 'DD')  ) b on a. dates = b.dates";
					
		
		
		return (List<Map<String, String>>) petDao.query(sql);
		
	}
	
	
	
	/**
	 * 按月统计
	 * @param start
	 * @param end
	 * @return 数据
	 */
	public List<Map<String,String>> statsByMonth(String start,String end){
		
		String sql ="select nvl(a.insums,0) as inmoney,nvl(b.outsums,0) as outmoney,nvl(a.dates,b.dates) as dates from "+
				"(select sum(t.sale_money) as insums, trunc(t.sale_date, 'MM') as dates from salehistory t "+
				"where to_char(t.sale_date,'yyyy/mm/dd hh24:mi:ss')>='"+start+"' and to_char(t.sale_date,'yyyy/mm/dd hh24:mi:ss')<'"+end+"' group by trunc(t.sale_date, 'MM') "+
				") a full join (select sum(c.animal_costprice) as outsums, trunc(c.animal_publictime, 'MM') as dates from animalsinfo c "+
				"where to_char(c.animal_publictime,'yyyy/mm/dd hh24:mi:ss')>='"+start+"' and to_char"
				+ "(c.animal_publictime,'yyyy/mm/dd hh24:mi:ss')<'"+end+"' group by trunc(c.animal_publictime, 'MM')  ) b on a. dates = b.dates";
					
		
		
		return (List<Map<String, String>>) petDao.query(sql);
		
	}
	
	/**
	 * 最近n天
	 * @return
	 */
	public List<Map<String,String>> statsWeekend(int day){
		String sql = "select nvl(a.insums,0) as inmoney,nvl(b.outsums,0) "
				+ "as outmoney,nvl(a.dates,b.dates) as dates from "
				+ "( select sum(t.sale_money) as insums, trunc(t.sale_date, 'DD') "
				+ "as dates from salehistory t where to_date(sysdate)-t.sale_date<="+day
				+ " group by trunc(t.sale_date, 'DD') ) a full join ( select sum(c.animal_costprice) "
				+ "as outsums, trunc(c.animal_publictime, 'DD') as dates from animalsinfo c "
				+ "where to_date(sysdate)-c.animal_publictime<="+day
				+ " group by trunc(c.animal_publictime, 'DD')  ) b on a. dates = b.dates ";
		
		return (List<Map<String, String>>) petDao.query(sql);
	}
	
	/**
	 * 销售排名
	 * @return
	 */
	public List<Map<String,String>> petBig(){
		String sql = "select t.id,t.animal_name,t.animal_boughttimes,rownum from animalsinfo t where rownum<10 order by t.animal_boughttimes desc  ";
		
		return (List<Map<String, String>>) petDao.query(sql);
	}
	
	/**
	 * 销售历史
	 * @return
	 */
	public List<Map<String,String>> recentForm(){
		String sql = "select t.animal_name as names, a.sale_date as dates from salehistory a left join animalsinfo t on a. animal_id=t.id and rownum<10 order by a.sale_date desc";
		return (List<Map<String, String>>) petDao.query(sql);
	}
	
	
	/**
	 * 待处理订单个数
	 * 新用户个数
	 */
	public List<Map<String,String>> otherSet(){
		String sql = "select * from (select count(*)"
				+ " as fcount from salehistory t where t.form_status='未处理'),"
				+ "(select count(*) as pcount from vipinfo v where to_char(v.vip_registdate,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd'))";
		return (List<Map<String, String>>) petDao.query(sql);
	}
	
	
}
